<?php

/**
 * @author Tan-Tan
 * @package SeoPortal
 * @subpackage Server
 */
class CrawlActivity extends Ezer_AsynchronousActivity
{
	public function execute(array $args)
	{
		$this->log("Executing Crawl Activity");
		
		$siteId = $args[CaseManager::VAR_SITE_ID];
		$reportId = $args[CaseManager::VAR_REPORT_ID];
		$this->log("Site [$siteId] Report [$reportId]");
		
		$site = SeoSitePeer::retrieveByPK($siteId);
		if(!$site)
		{
			$this->log("Site [$siteId] not found");
			return false;
		}
		
		$url = SeoUrlPeer::retrieveByPK($site->getUrlId());
		if(!$url)
		{
			$this->log("Site [" . $site->getUrlId() . "] not found");
			return false;
		}
			
		$maxPages = $site->getMaxPages();
		$paths = SeoSitePathPeer::retrieveBySiteId($siteId);
		
		$urls = UrlCrawler::crawl($url, $maxPages, $paths);
		$this->log("Crawled urls [" . count($urls) . "]");
		$url_ids = array();
		foreach($urls as $url)
		{
			$url->setReportId($reportId);
			$url->save();
			$url_ids[] = $url->getId();
		}
			
		$this->setVariable(CaseManager::VAR_URLS, $url_ids);
			
		return true;
	}
}
